<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/21
 * Time: 11:14
 */

namespace app\admin\controller;
use app\admin\model\Base;
use think\Controller;
use think\Db;
use think\Request;

class Userinfo extends Common
{
    public $obj="";
    public $table="userinfo";
    public $date="";
public $go="userinfo";
    public $file="new Common()";
    public function __initialize(){
        parent::__initialize();

    }
    public function show(){
        //每页显示10条数据
        $list = Db::table('userinfo')->order("id","desc")->paginate(3);
        $page=$list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        $this->assign('page',$page);
        // 渲染模板输出
        return $this->fetch($this->go.'/show');
    }











//添加数据页面
    public function add(){
        $list = Db::table('user')->select();
        $this->assign('list', $list);
        $goods = Db::table('goods')->select();
        $this->assign('goods', $goods);
        return $this->fetch();
    }
    //添加数据信息
    public function addm(){
        $user_name=input('user_name');
        if(!empty($user_name)){
            $object=new Base();
            $table=$this->table;
            $user_name=I('user_name');
            $sex=I('sex');
            $age=I('age');
            $user_image=$this->upload();
            $image=array($user_image);
            $images=implode("|",$image);
            //var_dump($images);die;
            $address_id=I('address_id');
            $money=I('money');
            $goods_collect=I('goods_collect');
            $re_collect=I('re_collect');
            $openid=I('openid');
            $sort=I('sort');
            $data=['user_name'=>$user_name,
                'sex'=>$sex,
                'age'=>$age,
                'address_id'=>$address_id,
                'user_image'=>$images,
                'money'=>$money,
                'goods_collect'=>$goods_collect,
                're_collect'=>$re_collect,
                'openid'=>$openid,
                'sort'=>$sort];
            //var_dump($data);die;
            $obj=$object->add($table,$data);
            if($obj){
                $this->success('添加成功', url('Userinfo/show'));
            }else {
                $this->error('添加失败');
            }
        }
    }
    //图片上传
    public function upload(){


        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        //dump($file);die;
        //图片信息整合:1,设置图片大小.2,移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

        if($info){
            // 成功上传后 获取上传信息
            $fileurl=$info->getSaveName();
            $fileurl=str_replace("\\","/",$fileurl);
            return $fileurl;
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }






//信息删除
    public function dels(){
        $obj=new Base();
        $id=input('id');
        //var_dump($id);
        $table=$this->table;
        $rs=$obj->del($table,$id);
        if($rs){
            $this->success('删除成功', url('Userinfo/show'));
        }else {
            $this->error('删除失败');
        }
    }











//修改内容页面
public function update(){
    $id=input('id');
    $info = Db::table('userinfo')->where("id=$id")->find();
    $this->assign('info', $info);
    $list = Db::table('user')->select();
    $this->assign('list', $list);
    $goods = Db::table('goods')->select();
    $this->assign('goods', $goods);
    return $this->fetch();
    }
    //修改内容
    public function updatem(){

        $object=new Base();
        $table=$this->table;
        $id=input('id');
        $user_name=I('user_name');
        $sex=I('sex');
        $age=I('age');
        $user_image=$this->upload();
        $image=array($user_image);
        $images=implode("|",$image);
        //var_dump($images);die;
        $address_id=I('address_id');
        $money=I('money');
        $goods_collect=I('goods_collect');
        $re_collect=I('re_collect');
        $openid=I('openid');
        $sort=I('sort');
        $data=['user_name'=>$user_name,
            'sex'=>$sex,
            'age'=>$age,
            'address_id'=>$address_id,
            'user_image'=>$images,
            'money'=>$money,
            'goods_collect'=>$goods_collect,
            're_collect'=>$re_collect,
            'openid'=>$openid,
            'sort'=>$sort];
        $obj=$object->upd($table,$id,$data);
        if($obj){
            $this->success('修改成功', url('Userinfo/show'));
        }else {
            $this->error('修改失败');
        }
    }












}

